package com.ruoyi.system.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.RecievedDataVo;
import com.ruoyi.system.domain.bo.RecievedDataBo;
import com.ruoyi.system.service.IRecievedDataService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 接收的数据帧
 *
 * @author ruoyi
 * @date 2024-06-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/data")
public class RecievedDataController extends BaseController {

    private final IRecievedDataService iRecievedDataService;

    /**
     * 查询接收的数据帧列表
     */
    @SaCheckPermission("system:data:list")
    @GetMapping("/list")
    public TableDataInfo<RecievedDataVo> list(RecievedDataBo bo, PageQuery pageQuery) {
        return iRecievedDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出接收的数据帧列表
     */
    @SaCheckPermission("system:data:export")
    @Log(title = "接收的数据帧", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RecievedDataBo bo, HttpServletResponse response) {
        List<RecievedDataVo> list = iRecievedDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "接收的数据帧", RecievedDataVo.class, response);
    }

    /**
     * 获取接收的数据帧详细信息
     *
     * @param dataFrameId 主键
     */
    @SaCheckPermission("system:data:query")
    @GetMapping("/{dataFrameId}")
    public R<RecievedDataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long dataFrameId) {
        return R.ok(iRecievedDataService.queryById(dataFrameId));
    }

    /**
     * 新增接收的数据帧
     */
    @SaCheckPermission("system:data:add")
    @Log(title = "接收的数据帧", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RecievedDataBo bo) {
        return toAjax(iRecievedDataService.insertByBo(bo));
    }

    /**
     * 修改接收的数据帧
     */
    @SaCheckPermission("system:data:edit")
    @Log(title = "接收的数据帧", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RecievedDataBo bo) {
        return toAjax(iRecievedDataService.updateByBo(bo));
    }

    /**
     * 删除接收的数据帧
     *
     * @param dataFrameIds 主键串
     */
    @SaCheckPermission("system:data:remove")
    @Log(title = "接收的数据帧", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dataFrameIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] dataFrameIds) {
        return toAjax(iRecievedDataService.deleteWithValidByIds(Arrays.asList(dataFrameIds), true));
    }
}
